﻿Public Class ClsControle
    Dim ErrorProvider1 As New ErrorProvider

    Public Function PermitirsoNumeros(ByVal tecla As String) As Boolean
        If Asc(tecla) = 8 Or Asc(tecla) = 13 Then
        ElseIf Asc(tecla) < 48 Or Asc(tecla) > 57 Then
            Return True
        End If
    End Function


    Public Function PermitirsoNumerosVirgula(ByVal tecla As String) As Boolean
        If Asc(tecla) = 8 Or Asc(tecla) = 13 Or Asc(tecla) = 44 Then
        ElseIf Asc(tecla) < 48 Or Asc(tecla) > 57 Then
            Return True
        End If
    End Function
    Public Function Testar_vazio(ByVal tela As Object) As Boolean
        Dim controle As Object
        For Each controle In tela.controls
            If TypeOf controle Is TextBox And controle.tag <> "x" Then
                If controle.Text = String.Empty Then
                    MsgBox("o campo: " & controle.Tag & " deve ser selecionado!!!")
                    controle.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(128, Byte))
                    ErrorProvider1.SetError(controle, "Preencha o campo!")
                    controle.focus()
                    Return True
                Else
                    controle.BackColor = System.Drawing.Color.White
                    ErrorProvider1.SetError(controle, String.Empty)

                End If
            End If
        Next
    End Function


    Public Function ValidaNumerico(ByVal controle As TextBox) As Boolean  'Verificamos se o Textbox está vazio  
        If Not IsNumeric(controle.Text) Then
            'Indicamos qual controle receberá a Mensagem de erro
            ErrorProvider1.SetError(controle, "Informe apenas números!")
            'Limpa a Mensagem de erro
            ErrorProvider1.SetError(controle, String.Empty)
            Return False
        Else
            Return True
        End If
    End Function



    ''' <summary> ''' Verifica o Preenchimento de um combo ''' </summary>
    Public Function Validacombo(ByVal controle As ComboBox) As Boolean

        If TypeOf controle Is ComboBox Then
            If controle.Text = String.Empty Or controle.Text = "(selecione a opção desejada)" Then
                MsgBox("O campo: " & controle.Tag & " deve ser selecionado!!!")
                'altera a cor de fundo
                controle.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(128, Byte))
                'Indicamos qual controle receberá a Mensagem de erro     
                ErrorProvider1.SetError(controle, "Selecione a opção!!!")
                controle.Focus()
                Return False
            End If
        Else
            controle.BackColor = System.Drawing.Color.White
            'Limpa a Mensagem de erro    
            ErrorProvider1.SetError(controle, String.Empty)
            Return True
        End If
    End Function
    Public Sub Limpar_tela(ByVal tela As Object)
        Dim controle As Object
        For Each controle In tela.controls
            If TypeOf controle Is TextBox Or TypeOf controle Is MaskedTextBox Or TypeOf controle Is ComboBox Then
                controle.Text = ""
            ElseIf TypeOf controle Is CheckBox Then
                controle.CHECKED = False
            End If
        Next
    End Sub
    Public Sub Habilitar_tela(ByVal tela As Object, ByVal tipo As Boolean)
        Dim controle As Object
        For Each controle In tela.controls
            If TypeOf controle Is TextBox Or TypeOf controle Is MaskedTextBox Or TypeOf controle Is ComboBox Or TypeOf controle Is CheckBox Or TypeOf controle Is DateTimePicker Or TypeOf controle Is DataGridView Then
                controle.enabled = tipo
            End If
        Next
    End Sub
    Public Sub Habilitar_botoes(ByVal tela As Object, ByVal tipo As Boolean)
        Dim controle As Object
        For Each controle In tela.controls
            If TypeOf controle Is Button Then
                If controle.name.ToString.ToUpper = "BTNGRA" Then
                    controle.ENABLED = Not tipo
                ElseIf controle.NAME.ToString.ToUpper = "BTNSAI" Or controle.name.ToString.ToUpper = "BTNCAN" Then
                    controle.enabled = True
                Else
                    controle.enabled = tipo
                End If
            End If
        Next
    End Sub
End Class
